<?php

namespace Database\Seeders;

use Illuminate\Database\Seeder;
use App\Models\Freelancer;
use App\Models\Task;
use App\Models\TaskApplication;
use Faker\Factory as Faker;

class FreelancerSeeder extends Seeder
{
    /**
     * Run the database seeds.
     *
     * @return void
     */
    public function run()
    {
        $faker = Faker::create('zh_CN');

        // 创建测试用户
        $testFreelancer = Freelancer::updateOrCreate(
            ['phone' => '13800138000'],
            [
                'name' => '测试用户',
                'email' => 'test@example.com',
                'id_card' => '110101199001010001',
                'gender' => 'male',
                'education' => 'bachelor',
                'age' => 25,
                'status' => 'active',
            ]
        );

        $this->command->info('Test freelancer created: ' . $testFreelancer->name);

        // 创建20个自由职业者
        for ($i = 0; $i < 20; $i++) {
            $gender = $faker->randomElement(['male', 'female']);
            $name = $gender === 'male' ? $faker->name('male') : $faker->name('female');
            
            $freelancer = Freelancer::create([
                'name' => $name,
                'phone' => $faker->unique()->phoneNumber,
                'email' => $faker->unique()->safeEmail,
                'id_card' => $faker->unique()->regexify('[1-9]\d{5}(19|20)\d{2}(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])\d{3}[\dX]'),
                'bank_account' => $faker->optional(0.7)->regexify('[1-9]\d{15,18}'),
                'bank_name' => $faker->optional(0.7)->randomElement([
                    '中国工商银行', '中国建设银行', '中国农业银行', '中国银行', 
                    '招商银行', '中信银行', '光大银行', '民生银行', '华夏银行', '平安银行'
                ]),
                'gender' => $gender,
                'education' => $faker->randomElement(['high_school', 'college', 'bachelor', 'master', 'doctor']),
                'age' => $faker->numberBetween(20, 50),
                'resume' => $faker->optional(0.8)->paragraph(3),
                'status' => $faker->randomElement(['active', 'inactive']),
                'id_card_recognition_status' => $faker->randomElement([
                    \App\Models\Freelancer::RECOGNITION_PENDING,
                    \App\Models\Freelancer::RECOGNITION_APPROVED,
                    \App\Models\Freelancer::RECOGNITION_REJECTED,
                ]),
                'face_recognition_status' => $faker->randomElement([
                    \App\Models\Freelancer::RECOGNITION_PENDING,
                    \App\Models\Freelancer::RECOGNITION_APPROVED,
                    \App\Models\Freelancer::RECOGNITION_REJECTED,
                ]),
                'created_at' => $faker->dateTimeBetween('-6 months', 'now'),
            ]);

            // 为一些自由职业者创建任务申请关联
            if ($faker->boolean(60)) { // 60%的概率有任务申请
                $tasks = Task::where('tenant_id', 1)->inRandomOrder()->limit($faker->numberBetween(1, 3))->get();
                
                foreach ($tasks as $task) {
                    // 检查是否已存在申请记录
                    $existingApplication = TaskApplication::where('freelancer_id', $freelancer->id)
                        ->where('task_id', $task->id)
                        ->first();
                        
                    if (!$existingApplication) {
                        TaskApplication::create([
                            'application_number' => 'APP' . now()->format('YmdHis') . str_pad($faker->unique()->numberBetween(1, 9999), 4, '0', STR_PAD_LEFT),
                            'task_id' => $task->id,
                            'tenant_id' => $task->tenant_id,
                            'freelancer_id' => $freelancer->id,
                            'name' => $freelancer->name,
                            'phone' => $freelancer->phone,
                            'email' => $freelancer->email,
                            'id_card' => $freelancer->id_card,
                            'gender' => $freelancer->gender,
                            'birth_date' => $faker->dateTimeBetween('-50 years', '-20 years')->format('Y-m-d'),
                            'address' => $faker->address,
                            'experience' => $faker->optional(0.7)->paragraph(2),
                            'skills' => json_encode($faker->randomElements([
                                '客服', '销售', '文案', '设计', '编程', '翻译', '数据录入', 
                                '市场调研', '电话营销', '内容审核'
                            ], $faker->numberBetween(1, 3))),
                            'education' => $freelancer->education,
                            'emergency_contact' => $faker->name,
                            'emergency_phone' => $faker->phoneNumber,
                            'status' => $faker->randomElement(['pending', 'approved', 'rejected', 'completed']),
                            'application_note' => $faker->optional(0.5)->sentence,
                            'rejection_reason' => $faker->optional(0.2)->sentence,
                            'applied_at' => $faker->dateTimeBetween('-3 months', 'now'),
                            'processed_at' => $faker->optional(0.7)->dateTimeBetween('-2 months', 'now'),
                        ]);
                    }
                }
            }
        }

        $this->command->info('FreelancerSeeder completed successfully!');
    }
}
